package com.framework.seedkit.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.framework.seedkit.R;
import com.framework.seedkit.base.BaseFragment;
import com.framework.seedkit.ui.uiframe.vpagerSlip.SelectBloodMonitorAct;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * author：FW on 2016/6/12 14:53
 * email：fengchangwen@f-road.com.cn
 */
public class TestFrag2 extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.label_measure_txt) TextView label_measure_txt;
    @BindView(R.id.label_tips_txt) TextView label_tips_txt;
    @BindView(R.id.label_result_txt) TextView label_result_txt;
    @BindView(R.id.label_report_txt) TextView label_report_txt;
    @BindView(R.id.start_measure_btn) Button start_measure_btn;

    private String TAG = "TestFrag2";
    private TextView tv;

    private Timer timer;
    private TimerTask timerTask;
    private int t = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_view2;
    }

    @Override
    public void initView() {
        label_measure_txt.setOnClickListener(this);
        label_tips_txt.setOnClickListener(this);
        label_result_txt.setOnClickListener(this);
        label_report_txt.setOnClickListener(this);
        start_measure_btn.setOnClickListener(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");

        initTimer();
    }

    public TestFrag2() {
    }

    public void initTimer () {
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
            timer = null;
        }
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (t > 100) {
                    t = 0;
                }
                t++;
                mHandler.sendEmptyMessage(0);
            }
        };
        timer.schedule(timerTask, 100, 1000);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (tv != null) {
                        tv.setText("current t is: " + t);
                    }
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.label_measure_txt:
                break;
            case R.id.label_tips_txt:
                break;
            case R.id.label_result_txt:
                break;
            case R.id.label_report_txt:
                break;
            case R.id.start_measure_btn:
                startActivity(new Intent(getActivity(), SelectBloodMonitorAct.class));
                break;
            default:
                break;
        }
    }
}
